package com.gfm.story.service.story;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.gfm.story.dao.BookDao;
import com.gfm.story.dao.CategoryDao;
import com.gfm.story.dao.ChapterDao;
import com.gfm.story.dao.DictionaryDao;
import com.gfm.story.entity.base.ResultBean;
import com.gfm.story.entity.dict.Dictionary;
import com.gfm.story.entity.story.Book;
import com.gfm.story.entity.story.Category;
import com.gfm.story.entity.story.Chapter;
import com.gfm.story.util.ReadTxtFile;
import com.sun.org.apache.xpath.internal.operations.Mod;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ValueConstants;

import javax.annotation.Resource;
import java.util.*;

/**
 * @BelongsProject: story
 * @BelongsPackage: com.gfm.story.service.story
 * @Author: Administrator
 * @CreateTime: 2019-06-03 15:20
 * @Description: ${Description}
 */
@Service
public class BookService {

    @Resource
    private BookDao bookDao;
    @Resource
    private CategoryDao categoryDao;
    @Resource
    private DictionaryDao dictionaryDao;
    @Resource
    private ChapterDao chapterDao;
    @Resource
    private  CategoryService categoryService;


    public Map<String,Object> getStoryList(Map<String, Object> map,Integer page) {
        List<Book> list = bookDao.getStoryPage(map);
        for (Book book:list){
            if (book.getCategoryId()!=null){
                //修改图书类型
                Category category = new Category();
                category.setId(book.getCategoryId());
                Category category1 = categoryDao.selectOne(category);
                book.setCategoryName(category1.getName());
            }else{
                book.setCategoryName("其他小说");
            }
        }
        Integer count =bookDao.getStoryPageCount(map);
        Map<String, Object> map1 = new HashMap<String,Object>();
        map1.put("total", (count+20-1)/20);
        map1.put("objs", list);
        map1.put("searchName", map.get("nameOrAuthor"));
        map1.put("currentPage", page);
        return map1;
    }





    public ResultBean getBookListByCategoryName(Map<String,Object> map) {
        List<Map> list = bookDao.getStoryListByIdsAndCategoryName(map);
        int count = bookDao.getStoryCountByIdsAndCategoryName(map);
        Map maps = new HashMap();
        maps.put("total", count);
        maps.put("objs", list);
        JSONObject json = JSONObject.fromObject(maps);
        return ResultBean.getSuccess(json);
    }

    //首页分类
    public Map<String, Object> getAllCategoryStoryList() {
        Dictionary dictionary = new Dictionary();
        dictionary.setDictCode("BookDomainAddress");
        dictionary = dictionaryDao.selectOne(dictionary);
        List<Category> categoryList = new ArrayList<Category>();
        categoryList = categoryService.getCategory();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("Frist:玄幻小说",null);
        map.put("Frist:修真小说",null);
        map.put("Frist:都市小说",null);
        map.put("Frist:穿越小说",null);
        map.put("Frist:网游小说",null);
        map.put("Frist:科幻小说",null);
        map.put("Frist:其他小说",null);
        map.put("Frist:newBook",null);
        map.put("newBook",null);
        map.put("recommend",null);
        if (categoryList!=null){
            for (Category category : categoryList){
                if (category.getId()!=null){
                    List<Book> list = bookDao.getBookListByCategory(category.getId());
                    if (!list.isEmpty()){
                        list.get(0).setImgUrl(dictionary.getDisc()+list.get(0).getImgUrl());
                        map.put("Frist:"+category.getName(),list.get(0));
                        list.remove(0);
                        map.put(category.getName(), list);
                    }
                }
            }
        }
        List<Book> newAddBook = bookDao.getNewBook();//30条最新入库book
        if (!newAddBook.isEmpty()){
            //数据字典中的服务器图片域名-地址
            Book book = new Book();
            book=newAddBook.get(0);
            if (book!=null){
                book.setImgUrl(dictionary.getDisc()+book.getImgUrl());
            }
            map.put("Frist:newBook",book);
            newAddBook.remove(0);
            map.put("newBook",newAddBook);
        }
        //好书推荐，推荐量最多的
        Book book = bookDao.getRecommendHot();
        if (book!=null){
            book.setImgUrl(dictionary.getDisc()+book.getImgUrl());
        }
        map.put("recommend",book);
        return map;
    }

    public Map<String, Object> getBookListByStatus(Map<String, Object> map,Integer page) {
        List<Book> list = bookDao.getBookListByStatus(map);
        List<Book> books = new ArrayList<Book>();
        for (Book book:list){//查询小说所属类型
            if (book.getCategoryId()==null){
                book.setCategoryName("其他小说");
            }else{
                Category category = new Category();
                category.setId(book.getCategoryId());
                Category category1 = categoryDao.selectOne(category);
                book.setCategoryName(category1.getName());
            }
            books.add(book);
        }
        int count = bookDao.getBookCountByStatus(map);
        Map maps = new HashMap();
        maps.put("total", (count+20-1)/20);
        maps.put("currentPage", page);
        maps.put("objs", books);
        maps.put("nameOrAuthor",map.get("nameOrAuthor"));
        return maps;
    }

    //最新入库
    public Map<String, Object> getNewBookList(Map<String, Object> map,Integer page) {
        List<Book> list = bookDao.getNewBookList(map);
        for (Book book:list){
            System.out.println(book.toString());
        }
        for (Book book:list){
            if (book.getCategoryId()!=null){
                //修改图书类型
                Category category = new Category();
                category.setId(book.getCategoryId());
                Category category1 = categoryDao.selectOne(category);
                book.setCategoryName(category1.getName());
            }else{
                book.setCategoryName("其他小说");
            }
        }
        int count = bookDao.getNewBookCount(map);
        Map maps = new HashMap();
        maps.put("total", (count+20-1)/20);
        maps.put("currentPage", page);
        maps.put("objs", list);
        return maps;
    }


    //阅读小说1.小说首页阅读，2.指定章节阅读
    public Map<String,Object> getChapterInfo(Long chapterId, Long bookId) {
        Map<String,Object> map = new HashMap<String,Object>();
        //从首页阅读（没有指定章节）
        if (chapterId==null){
            //1.根据bookId查询所有章节，获取第一章(根据时间正序排序得到第一位)
            Chapter chapter = new Chapter();
            chapter.setBookId(bookId);
            EntityWrapper<Chapter> wrapper = new EntityWrapper<Chapter>();
            wrapper.eq("book_id", bookId);
            wrapper.orderBy("update_time");
            List<Chapter> chapters = chapter.selectList(wrapper);
            Chapter chapter1 = new Chapter();
            String content ="";
            if (!chapters.isEmpty()){//存在章节
                chapter1 = chapters.get(0);//获取第一章
                //根据章节信息解析处章节内容
                ReadTxtFile read = new ReadTxtFile();
                content = read.readTxtFile(chapter1.getContentAddress());
            }
            map.put("content", content);
            map.put("chapterInfo", chapter1);
        }else{//从指定章节阅读
            Chapter chapter = new Chapter();
            chapter.setBookId(bookId);
            chapter.setId(chapterId);
            Chapter chapter1 = chapterDao.selectOne(chapter);
            String content = "";
            if (chapter1!=null){//存在章节
                //根据章节信息解析处章节内容
                ReadTxtFile read = new ReadTxtFile();
                content = read.readTxtFile(chapter1.getContentAddress());
            }
            map.put("content", content);
            map.put("chapterInfo", chapter1);
        }
        return map;
    }

    //上一章
    public Map<String,Object> forward(Long chapterId, Long bookId) {
        Map<String,Object> map = new HashMap<String,Object>();
        //2.当前chapterid--，再次查询该章节，并解析路径返回content
        Chapter chapter = new Chapter();
        chapter.setId(chapterId-1);
        Chapter chapter1 = chapter.selectById();
        String content = "";
        if (chapter1!=null){//存在章节
            //根据章节信息解析处章节内容
            ReadTxtFile read = new ReadTxtFile();
            content = read.readTxtFile(chapter1.getContentAddress());
        }
        map.put("chapterInfo", chapter1);
        map.put("content", content);
        return map;
    }

    //下一章
    public Map<String,Object> Back(Long chapterId, Long bookId) {
        Map<String,Object> map = new HashMap<String,Object>();
        //2.当前chapterid--，再次查询该章节，并解析路径返回content
        Chapter chapter = new Chapter();
        chapter.setId(chapterId+1);
        Chapter chapter1 = chapter.selectById();
        String content = "";
        if (chapter1!=null){//存在章节
            //根据章节信息解析处章节内容
            ReadTxtFile read = new ReadTxtFile();
            content = read.readTxtFile(chapter1.getContentAddress());
        }
        map.put("chapterInfo", chapter1);
        map.put("content", content);
        System.out.println(content);
        return map;
    }


}
